package com.googlecode.imule.apps;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;
import java.util.Scanner;

import javax.media.MediaLocator;

import org.apache.commons.digester.Digester;
import org.xml.sax.SAXException;

import com.apple.dnssd.DNSSDException;
import com.googlecode.imule.apps.util.ListenerX;
import com.googlecode.imule.data.Action;
import com.googlecode.imule.data.Parse;
import com.googlecode.imule.data.UDPCommunicationX;
import com.googlecode.imule.discovery.Contact;
import com.googlecode.imule.discovery.DiscoveryServiceX;
import com.googlecode.imule.media.IPlayer;
import com.googlecode.imule.media.IStream;
import com.googlecode.imule.redundant.Components;

/**
 * A component that combines the discovery, communication modules of
 * the imule platform.
 * 
 * @author Neil Cowzer
 */
public class Z extends X{
	IPlayer player;
	
	public String streamingTO;
	
	public Z(String name) throws SocketException, DNSSDException {
		super(name);
	}
	
	@Override
	public void setOn(boolean status) {
		System.out.println("playing status: "+ status);
		if (status){
			if (player != null)
				player.stop();
			Contact contact = discovery.getContacts().getContact(streamingTO);
			
			player = new IPlayer(contact.getIpAddress().toString());
			
			player.start();
		} else {
			player.stop();
			System.out.println("stopping...");
		}
	}
	
	@Override
	public void execute(String xml) {
		Action action = null;

		try {
			action = Parse.action(xml);
			
			if (action.getName().equalsIgnoreCase("setOn")){
				if (action.getArguments().get(0).getValue().equalsIgnoreCase("true")){
					streamingTO = action.getId();
					setOn(true);
				} else
					setOn(false);
			}
			
		} catch (IOException e){
		} catch (SAXException e){}
	}
	
	public static void main(String[] args) throws DNSSDException, IOException {
		Scanner scanner = new Scanner(System.in);

		System.out.println("player name");
		Z test = new Z(scanner.nextLine());
		test.setManifest("resources/manifests/light.xml");
		System.out.println(test.getManifest());
		String input = "";

		while(!input.equalsIgnoreCase("close")){
			input = scanner.nextLine();
			if(input.charAt(0) == '@'){
				String[] split = input.split("\\s");
				String serviceName = split[0].substring(1, split[0].length());
				if (split[1].equalsIgnoreCase("@ON")){
					test.sendAction(test.name, "setOn", "true");
					System.out.println(test.name);
				} else if (split[1].equalsIgnoreCase("@OFF")) {
					test.sendAction(serviceName, "setOn", "false");
				} else {
					String message = "";
					for (int i = 1; i < split.length; i++) {
						message += split[i] + " ";
					}
					test.send(message, serviceName);
				}
			} else {
				test.broadcast(input);
			}
		}
		System.out.println("closing");

		System.exit(0);
	}
}
